<?php
namespace app\index\controller;
use app\common\controller\HomeBase;
use app\common\model\Article;
use think\Cache;
use think\Db;

class Job extends HomeBase
{
    protected $siteConfig;
    public function _initialize()
    {
        parent::_initialize();
        $this->siteConfig = Cache::get('site_config');
    }
    public function scheduledRelease()
    {
        $updatekey = input('updatekey');
        if (!$updatekey) {
            return json(array('code' => 0, 'msg' => '缺少updatekey参数'));
        }
        $res = Db::name('system')->where('name', 'otaservice')->find();
        $otaInfo = unserialize($res['value']);
        if ($otaInfo['updatekey'] != $updatekey) {
            return json(array('code' => 0, 'msg' => 'updatekey不正确'));
        }
        $updatedAt=date('Y-m-d H:i:s');
        $map['status'] = 0;
        $map['publish_time'] = ['lt',$updatedAt];
        $map['publish_time'] = ['exp',Db::raw('>updated_at')];
        $count=(new Article())->where($map)->count();
        if(!$count){
            return json(array('code' => 0, 'msg' => '没有需要发布的内容'));
        }
        $setField['status']=1;
        $setField['updated_at']=$updatedAt;
        (new Article())->where($map)->setField($setField);
    }
}
